package com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.restful;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.authority.starter.annotation.SkipAuthority;
import com.smedi.ismedi.dangeroussourcemanage.api.dto.request.*;
import com.smedi.ismedi.dangeroussourcemanage.api.dto.response.*;
import com.smedi.ismedi.dangeroussourcemanage.api.feign.RemoteDangerousSourceManageFeignApi;
import com.smedi.ismedi.dangeroussourcemanage.core.application.command.*;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.DangerousSourceLibraryQueryService;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.DangerousSourceManageQueryService;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerSourceVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceLibraryVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceListVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.query.vo.DangerousSourceManageDetailQueryVo;
import com.smedi.ismedi.dangeroussourcemanage.core.application.service.DangerousSourceManageApplicationService;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.database.starter.annotation.SkipSqlPermission;
import com.smedi.ismedi.web.starter.common.PaginationData;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 危险源Controller
 *
 * @author xienannan
 * @date 2021/08/23
 */
@AllArgsConstructor
@RestController
public class DangerousSourceManageController implements RemoteDangerousSourceManageFeignApi {
    private final DangerousSourceManageQueryService dangerousSourceManageQueryService;
    private final DangerousSourceManageApplicationService dangerousSourceManageApplicationService;
    private final DangerousSourceLibraryQueryService dangerousSourceLibraryQueryService;

    private final DozerUtil dozerUtil;

    private final AdapterConverter adapterConverter;

    @Override
    public ResponseData<DangerousSourceDetailResponse> getDangerousSourceDetail(Long dangerousSourceId) {
        DangerousSourceManageDetailQueryVo dangerousSourceManageDetailQueryVo = dangerousSourceManageQueryService.getQueryDangerousSourceManageDetail(dangerousSourceId);
        if (dangerousSourceManageDetailQueryVo == null) {
            return null;
        }

        DangerousSourceDetailResponse dangerousSourceDetailResponse = dozerUtil.convertor(dangerousSourceManageDetailQueryVo, DangerousSourceDetailResponse.class);
        dangerousSourceDetailResponse.setDangerousSourceLibraryResponse(dozerUtil.convertor(dangerousSourceManageDetailQueryVo.getDangerousSourceLibraryVo(), DangerousSourceLibraryResponse.class));

        return new ResponseData<DangerousSourceDetailResponse>().success(dangerousSourceDetailResponse);
    }

    @Override
    public ResponseData<Long> postAddDangerousSource(DangerousSourceManageRequest dangerousSourceManageRequest) {
        DangerousSourceManageCommand dangerousSourceManageCommand = dozerUtil.convertor(dangerousSourceManageRequest, DangerousSourceManageCommand.class);

        return new ResponseData<Long>().success(dangerousSourceManageApplicationService.addDangerousSource(dangerousSourceManageCommand));
    }

    @Override
    public ResponseData<Long> getDeleteDangerousSource(List<Long> dangerousSourceIds) {
        return new ResponseData<DangerousSourceDetailResponse>().success(dangerousSourceManageApplicationService.deleteDangerousSourceById(dangerousSourceIds));
    }

    @Override
    public ResponseData<PaginationData<QueryDangerousSourceListResponse>> postQueryDangerousSourceManagePageList(QueryDangerousSourcePageListRequest dangerousSourcePageListRequest) {
        Page<QueryDangerousSourceListResponse> dangerousSourceListResponsePage = new Page<>(dangerousSourcePageListRequest.getPageNum(),dangerousSourcePageListRequest.getPageSize());

        QueryDangerousSourcePageListCommand queryDangerousSourcePageListCommand = dozerUtil.convertor(dangerousSourcePageListRequest, QueryDangerousSourcePageListCommand.class);
        Page<DangerousSourceListVo> dangerousSourceListVoPage = dangerousSourceManageQueryService.postQueryDangerousSourceManagePageList(queryDangerousSourcePageListCommand);
        if (dangerousSourceListVoPage == null) {
            return null;
        }

        List<QueryDangerousSourceListResponse> dangerousSourceListResponseList = new ArrayList<>();
        for (DangerousSourceListVo dangerousSourceListVo:dangerousSourceListVoPage.getRecords()) {
            QueryDangerousSourceListResponse queryDangerousSourceListResponse = dozerUtil.convertor(dangerousSourceListVo,QueryDangerousSourceListResponse.class);
            queryDangerousSourceListResponse.setDangerousSourceLibraryResponse(dozerUtil.convertor(dangerousSourceListVo.getDangerousSourceLibraryVo(),DangerousSourceLibraryResponse.class));
            dangerousSourceListResponseList.add(queryDangerousSourceListResponse);
        }

        dangerousSourceListResponsePage.setRecords(dangerousSourceListResponseList);
        dangerousSourceListResponsePage.setTotal(dangerousSourceListVoPage.getTotal());

        PaginationData<QueryDangerousSourceListResponse> queryDangerousSourceListResponsePaginationData = dozerUtil.convertor(dangerousSourceListResponsePage,PaginationData.class);

        return new ResponseData<PaginationData<QueryDangerousSourceListResponse>>().success(queryDangerousSourceListResponsePaginationData);
    }

    @Override
    public ResponseData<List<QueryDangerousSourceListResponse>> queryDangerousSourceManageList(DangerousSourceListRequest dangerousSourceListRequest) {
        DangerousSourceListCommand dangerousSourceListCommand = dozerUtil.convertor(dangerousSourceListRequest, DangerousSourceListCommand.class);
        List<DangerousSourceListVo> dangerousSourceListVos = dangerousSourceManageQueryService.postQueryDangerousSourceManageList(dangerousSourceListCommand);
        if (dangerousSourceListVos == null){
            return null;
        }
        List<QueryDangerousSourceListResponse> dangerousSourceListResponseList = new ArrayList<>();
        for (DangerousSourceListVo dangerousSourceListVo : dangerousSourceListVos){
            QueryDangerousSourceListResponse queryDangerousSourceListResponse = dozerUtil.convertor(dangerousSourceListVo,QueryDangerousSourceListResponse.class);
            queryDangerousSourceListResponse.setDangerousSourceLibraryResponse(dozerUtil.convertor(dangerousSourceListVo.getDangerousSourceLibraryVo(),DangerousSourceLibraryResponse.class));
            dangerousSourceListResponseList.add(queryDangerousSourceListResponse);
        }
        return new ResponseData<List<QueryDangerousSourceListResponse>>().success(dangerousSourceListResponseList);
    }

    @Override
    public ResponseData<PaginationData<QueryDangerousSourceLibraryResponse>> postQueryDangerousSourceLibraryPageList(QueryDangerousSourceLibraryPageListRequest queryDangerousSourceLibraryPageListRequest) {
        Page<QueryDangerousSourceLibraryResponse> dangerousSourceLibraryResponsePage = new Page<>(queryDangerousSourceLibraryPageListRequest.getPageNum(), queryDangerousSourceLibraryPageListRequest.getPageSize());

        QueryDangerousSourceLibraryCommand queryDangerousSourceLibraryCommand = dozerUtil.convertor(queryDangerousSourceLibraryPageListRequest, QueryDangerousSourceLibraryCommand.class);
        Page<DangerousSourceLibraryVo> dangerousSourceLibraryVoPage = dangerousSourceLibraryQueryService.postQueryDangerousSourceLibraryPageList(queryDangerousSourceLibraryCommand);
        if (dangerousSourceLibraryVoPage == null) {
            return null;
        }

        List<DangerousSourceLibraryVo> dangerousSourceLibraryVoList = dangerousSourceLibraryVoPage.getRecords();

        dangerousSourceLibraryResponsePage.setRecords(dozerUtil.convertor(dangerousSourceLibraryVoList, QueryDangerousSourceLibraryResponse.class));
        dangerousSourceLibraryResponsePage.setTotal(dangerousSourceLibraryVoPage.getTotal());

        PaginationData<QueryDangerousSourceLibraryResponse> queryDangerousSourceLibraryResponsePaginationData = dozerUtil.convertor(dangerousSourceLibraryResponsePage,PaginationData.class);

        return new ResponseData<PaginationData<QueryDangerousSourceListResponse>>().success(queryDangerousSourceLibraryResponsePaginationData);
    }

    @Override
    public ResponseData<List<QueryDangerousSourceLibraryResponse>> postQueryDangerousSourceLibraryList(QueryDangerousSourceLibraryListRequest queryDangerousSourceLibraryListRequest) {
        QueryDangerousSourceLibraryCommand queryDangerousSourceLibraryCommand = dozerUtil.convertor(queryDangerousSourceLibraryListRequest, QueryDangerousSourceLibraryCommand.class);

        List<DangerousSourceLibraryVo> dangerousSourceLibraryVoList = dangerousSourceLibraryQueryService.postQueryDangerousSourceLibraryList(queryDangerousSourceLibraryCommand);
        if (dangerousSourceLibraryVoList == null) {
            return null;
        }

        List<QueryDangerousSourceLibraryResponse> queryDangerousSourceLibraryResponseList = dozerUtil.convertor(dangerousSourceLibraryVoList,QueryDangerousSourceLibraryResponse.class);
        return new ResponseData<List<QueryDangerousSourceListResponse>>().success(queryDangerousSourceLibraryResponseList);
    }

    @Override
    @SkipSqlPermission
    @SkipAuthority
    public ResponseEntity<byte[]> exportList(List<Long> dangerousSourceIds) throws IOException {
        return dangerousSourceManageApplicationService.exportDangerousSourceList(dangerousSourceIds);
    }

    @Override
    public ResponseData<PaginationData<QueryDangerousSourceListResponse>> postMobileQueryDangerousSourceManagePageList(MobileQueryDangerousSourceListRequest mobileQueryDangerousSourceListRequest) {
        Page<QueryDangerousSourceListResponse> dangerousSourceListResponsePage = new Page<>(mobileQueryDangerousSourceListRequest.getPageNum(), mobileQueryDangerousSourceListRequest.getPageSize());

        MobileQueryDangerousSourceListCommand mobileQueryDangerousSourceListCommand = dozerUtil.convertor(mobileQueryDangerousSourceListRequest, MobileQueryDangerousSourceListCommand.class);
        Page<DangerousSourceListVo> dangerousSourceListVoPage = dangerousSourceManageQueryService.mobileQueryDangerousSourceManagePageList(mobileQueryDangerousSourceListCommand);
        if (dangerousSourceListVoPage == null) {
            return null;
        }

        List<QueryDangerousSourceListResponse> dangerousSourceListResponseList = new ArrayList<>();
        for (DangerousSourceListVo dangerousSourceListVo : dangerousSourceListVoPage.getRecords()) {
            QueryDangerousSourceListResponse queryDangerousSourceListResponse = dozerUtil.convertor(dangerousSourceListVo, QueryDangerousSourceListResponse.class);
            queryDangerousSourceListResponse.setDangerousSourceLibraryResponse(dozerUtil.convertor(dangerousSourceListVo.getDangerousSourceLibraryVo(), DangerousSourceLibraryResponse.class));
            dangerousSourceListResponseList.add(queryDangerousSourceListResponse);
        }

        dangerousSourceListResponsePage.setRecords(dangerousSourceListResponseList);
        dangerousSourceListResponsePage.setTotal(dangerousSourceListVoPage.getTotal());

        PaginationData<QueryDangerousSourceListResponse> queryDangerousSourceListResponsePaginationData = dozerUtil.convertor(dangerousSourceListResponsePage, PaginationData.class);

        return new ResponseData<PaginationData<QueryDangerousSourceListResponse>>().success(queryDangerousSourceListResponsePaginationData);
    }

    @Override
    public ResponseData<List<DangerSourceResponse>> getDangerSourceList(DangerSourceListRequest dangerSourceListRequest) {
        List<DangerSourceVo> dangerSourceVoList = dangerousSourceManageQueryService.getDangerSourceList(dangerSourceListRequest);
        List<DangerSourceResponse> dangerSourceResponses = dozerUtil.convertor(dangerSourceVoList,DangerSourceResponse.class);
        return new ResponseData<DangerSourceResponse>().success(dangerSourceResponses);

    }

    @Override
    public ResponseData<Integer> getDangerSourceCount(DangerSourceListRequest dangerSourceListRequest) {
        return new ResponseData<>().success(dangerousSourceManageQueryService.getDangerSourceCount(dangerSourceListRequest));
    }

    @Override
    public ResponseData<Integer> getDangerSourceCountByTimeLine(DangerSourceTimeLineRequest dangerSourceTimeLineRequest) {
        TimeLineCommand timeLineCommand = adapterConverter.dangerSourceTimeLineRequestToTimeLineCommand(dangerSourceTimeLineRequest);
        return new ResponseData<>().success(dangerousSourceManageQueryService.getDangerSourceCountByTimeLine(timeLineCommand));
    }
}
